package com.stormragetech.arthas.process.context;

import com.stormragetech.arthas.common.exception.ArthasException;
import com.stormragetech.arthas.common.util.ArthasUtil;
import com.stormragetech.arthas.common.util.Assert;
import com.stormragetech.arthas.process.factory.ActionFactory;
import com.stormragetech.arthas.process.factory.ActionWrapper;
import com.stormragetech.arthas.process.factory.AwareActionFactory;
import com.stormragetech.arthas.process.invocation.ActionInvoke;
import com.stormragetech.arthas.process.proxy.FactoryProxy;
import com.stormragetech.arthas.process.proxy.Proxy;
import lombok.extern.log4j.Log4j2;

import java.net.URL;

/**
 * description: initalize context from xml config file
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
public class XmlConfigFileActionContext extends ActionContext implements Context {

    private ActionFactory factory;

    public XmlConfigFileActionContext(String name) {
        Assert.notNull(name, "file name cannot be null !!!");
        factory = new AwareActionFactory(wrapperMap(ArthasUtil.parsePackage(name)));
        log.info("action factory is: {}", factory.toString());
    }

    @SuppressWarnings("unchecked")
    @Override
    public Proxy getProxy(ActionWrapper action) {
        Proxy proxy = new FactoryProxy(action);
        proxy.setInvoke(new ActionInvoke(proxy));
        return proxy;
    }

    @Override
    public ActionWrapper getWrapper(String path) throws ArthasException {
        return factory.getWrapper(path);
    }

    @Override
    public Object getAction(String name) throws ArthasException {
        return factory.getAction(name);
    }

    @Override
    public Object getAction(String name, Class<?> clazz) throws ArthasException {
        return factory.getAction(name, clazz);
    }
}
